home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / bserverdir / sources / clients / displayiff.c < prev    next >
C/C++ Source or Header  |  1994-12-08  |  9KB  |  423 lines

  1. ; /*
  2. sc displayiff.c DATA=FAR NMINC STRMERGE NOSTKCHK NOSTDIO IGNORE=73 STRUCTUREEQUIVALENCE
  3. slink from lib:c.o displayiff.o to //Clients/DisplayIFF lib /lib/client.lib lib:sc.lib lib:amiga.lib SC SD NOICONS STRIPDEBUG
  4. delete displayiff.o
  5. quit
  6.  
  7.  DisplayIFF 1.2  (Client for BServer)
  8.  
  9.  Copyright © 1994 Stefano Reksten of 3AM - The Three Amigos!!!
  10.  All rights reserved.
  11.  
  12. */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <intuition/intuition.h>
  17. #include <datatypes/pictureclass.h>
  18.  
  19. #include <clib/exec_protos.h>
  20. #include <clib/intuition_protos.h>
  21. #include <clib/graphics_protos.h>
  22. #include <clib/dos_protos.h>
  23. #include <clib/alib_protos.h>
  24. #include <clib/icon_protos.h>
  25. #include <clib/asl_protos.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include <stdio.h>
  29.  
  30. #include "/include/client.h"
  31. #include "/include/bitmap/bitmap.h"
  32. #include "/include/bitmap/bitmap_pragmas.h"
  33.  
  34. #define BADFLAGS (SPRITES|VP_HIDE|GENLOCK_AUDIO|GENLOCK_VIDEO)
  35. #define FLAGMASK (~BADFLAGS)
  36. #define CAMGMASK (FLAGMASK & 0x0000FFFFL)
  37.  
  38. #define BMHDB_CMAPOK    7
  39. #define BMHDF_CMAPOK    (1 << BMHDB_CMAPOK)
  40.  
  41. struct IntuitionBase *IntuitionBase;
  42. struct GfxBase *GfxBase;
  43. struct Library *BitMapBase, *IconBase;
  44.  
  45. extern ULONG RangeSeed;
  46.  
  47. struct Screen *scr;
  48. struct BitMap *iffbmap;
  49. UBYTE *iffctable;
  50. ULONG color_count, displayID;
  51. char filename[128], drawername[128];
  52. BOOL domask, iff_loaded;
  53. struct BitMapHeader bmhd;
  54. BPTR iffhandle;
  55.  
  56. UWORD top, left, delay = 1;
  57. UBYTE frameptr = 0;
  58. struct BitMap *bmap, *scalebmap;
  59.  
  60. struct DisplayIDInformation *dinfo;
  61. struct BitMapScaleInfo myscaleinfo = {
  62.     NULL, NULL, NULL, 0, 0, 0, 0,
  63.     0, 0, 0, 0, 0, 0, NULL };
  64.  
  65.  
  66. BOOL GetPicName( void )
  67. {
  68. struct DiskObject *dobj;
  69. char **ttypes;
  70. char *picname;
  71. BOOL result = FALSE;
  72.  
  73. if ( IconBase = OpenLibrary( "icon.library", 0L ) )
  74.     {
  75.     if ( dobj = GetDiskObject( "DisplayIFF" ) )
  76.         {
  77.         ttypes = dobj->do_ToolTypes;
  78.             if ( picname = FindToolType( ttypes, "PICTURE" ) )
  79.                 {
  80.                 strcpy( filename, picname );
  81.                 strcpy( drawername, picname );
  82.                 *(PathPart(drawername)) = 0;
  83.                 result = TRUE;
  84.                 }
  85.  
  86.         domask = (BOOL)FindToolType( ttypes, "MASK" );
  87.  
  88.         FreeDiskObject( dobj );
  89.         }
  90.     CloseLibrary( IconBase );
  91.     }
  92. return result;
  93. }
  94.  
  95.  
  96. void DisposeIFF( void )
  97. {
  98. if ( iffbmap )
  99.     {
  100.     FreeBitMap( iffbmap );
  101.     iffbmap = NULL;
  102.     }
  103. if ( iffctable )
  104.     {
  105.     FreeVec( iffctable );
  106.     iffctable = NULL;
  107.     }
  108. }
  109.  
  110.  
  111. BOOL ReadUncompressedFile( void )
  112. {
  113. UBYTE *address, plane;
  114. UWORD row, bytesperrow;
  115.  
  116. bytesperrow = ((bmhd.bmh_Width+15)>>4)<<1;
  117.  
  118. for ( row = 0; row < bmhd.bmh_Height; row++ )
  119.     {
  120.     for ( plane = 0; plane < bmhd.bmh_Depth; plane++ )
  121.         {
  122.         address = iffbmap->Planes[plane] + row * iffbmap->BytesPerRow;
  123.         if ( Read( iffhandle, address, bytesperrow ) != bytesperrow )
  124.             return FALSE;
  125.         }
  126.     }
  127. return TRUE;
  128. }
  129.  
  130.  
  131. BOOL ReadByteRun1( ULONG body_length )
  132. {
  133. BYTE *memory;
  134. register BYTE *source;
  135. register UBYTE plane, counter, *dest;
  136. register UWORD row, bytesperrow, rowcounter;
  137. BOOL result = FALSE;
  138.  
  139. if ( memory=(UBYTE *)AllocMem( body_length, MEMF_ANY|MEMF_CLEAR ) )
  140.     {
  141.     if ( body_length = Read( iffhandle, memory, body_length ) )
  142.         {
  143.         source = memory;
  144.  
  145.         bytesperrow = ((bmhd.bmh_Width+15)>>4)<<1;
  146.  
  147.         for ( row = 0; row < bmhd.bmh_Height; row++ )
  148.             {
  149.             for ( plane = 0; plane < bmhd.bmh_Depth; plane++ )
  150.                 {
  151.                 dest = iffbmap->Planes[plane] + row * iffbmap->BytesPerRow;
  152.                 for ( rowcounter = 0; rowcounter < bytesperrow; )
  153.                     {
  154.                     if ( *source == -128 )
  155.                         source++;
  156.                     else
  157.                     if ( *source >= 0 )
  158.                         {
  159.                         counter = *source++ + 1;
  160.                         rowcounter += counter;
  161.                         while( counter )
  162.                             {
  163.                             *dest++ = *source++;
  164.                             counter--;
  165.                             }
  166.                         }
  167.                     else
  168.                         {
  169.                         counter = 1 - *source++;
  170.                         rowcounter += counter;
  171.                         while( counter )
  172.                             {
  173.                             *dest++ = *source;
  174.                             counter--;
  175.                             }
  176.                         source++;
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.         FreeMem( memory, body_length );
  182.         result = TRUE;
  183.         }
  184.     }
  185. return result;
  186. }
  187.     
  188.  
  189. UBYTE ReadByte( void ) {
  190. UBYTE my_byte;
  191. Read( iffhandle, &my_byte, sizeof(UBYTE) );
  192. return( my_byte ); }
  193.  
  194.  
  195. ULONG ReadLong( void ) {
  196. ULONG my_long;
  197. Read( iffhandle, &my_long, sizeof(ULONG) );
  198. return( my_long ); }
  199.  
  200.  
  201. BOOL DecodeILBM( char *filename )
  202. {
  203. ULONG    total_read = 12, form_length, chunk_found, chunk_length;
  204. UWORD    n;
  205. BOOL    found_body = FALSE;
  206.  
  207. iff_loaded = FALSE;
  208.  
  209. if ( !( iffhandle = Open( filename, MODE_OLDFILE ) ) )
  210.     goto end;
  211.  
  212. if ( ReadLong()!=ID_FORM )
  213.     goto end;
  214.  
  215. form_length = ReadLong();
  216.  
  217. if ( ReadLong()!=ID_ILBM )
  218.     goto end;
  219.  
  220. do
  221.     {
  222.     chunk_found = ReadLong();
  223.     chunk_length = ReadLong();
  224.     total_read += 4 + chunk_length;
  225.  
  226.     switch( chunk_found )
  227.         {
  228.         case    ID_BMHD:
  229.             Read( iffhandle, &bmhd, sizeof(struct BitMapHeader) );
  230.             if ( bmhd.bmh_Compression > 1 )
  231.                 goto end;
  232.             if ( bmhd.bmh_Depth == 8 && domask )
  233.                 goto end;
  234.             myscaleinfo.bsi_Width = bmhd.bmh_Width;
  235.             myscaleinfo.bsi_Height = bmhd.bmh_Height;
  236.             break;
  237.         case    ID_CAMG:
  238.             displayID = ReadLong() & CAMGMASK;
  239.             if ( displayID & HAM )
  240.                 goto end;
  241.             break;
  242.         case    ID_CMAP:
  243.             color_count = chunk_length/3;
  244.             if ( displayID & EXTRA_HALFBRITE )
  245.                 color_count >>= 1;
  246.             if ( iffctable = AllocVec( chunk_length, MEMF_ANY | MEMF_CLEAR ) )
  247.             Read( iffhandle, iffctable, chunk_length );
  248.             if ( !(bmhd.bmh_Pad & BMHDF_CMAPOK) )
  249.                 for ( n = 0; n < chunk_length; n++ )
  250.                     *(iffctable+n) = (*(iffctable+n) & 0xF0) + (*(iffctable+n)>>4);
  251.             break;
  252.         case    ID_BODY:
  253.             found_body = TRUE;
  254.  
  255.             if ( !(iffbmap = CreateBitMap( bmhd.bmh_Width, bmhd.bmh_Height, bmhd.bmh_Depth + (domask?1:0) ) ) )
  256.                 goto end;
  257.             
  258.             if ( bmhd.bmh_Compression )
  259.                 iff_loaded = ReadByteRun1( chunk_length );
  260.             else
  261.                 iff_loaded = ReadUncompressedFile();
  262.  
  263.             if ( iff_loaded && domask )
  264.                 {
  265.                 register UBYTE plane, *srcaddress, *dstaddress, bmhbpr;
  266.                 register UWORD byte, row;
  267.  
  268.                 bmhbpr = ((bmhd.bmh_Width+15)>>4)<<1;
  269.                 for ( row = 0; row < bmhd.bmh_Height; row++ )
  270.                     for ( plane = 0; plane < bmhd.bmh_Depth; plane++ )
  271.                         {
  272.                         dstaddress = iffbmap->Planes[bmhd.bmh_Depth] + iffbmap->BytesPerRow * row;
  273.                         srcaddress = iffbmap->Planes[plane] + iffbmap->BytesPerRow * row;
  274.                         for ( byte = 0; byte < bmhbpr; byte++ )
  275.                             *dstaddress++ |= *srcaddress++;
  276.                         }
  277.                 }
  278.  
  279.             Close( iffhandle ); iffhandle = NULL;
  280.             break;
  281.         default:
  282.             Seek( iffhandle, chunk_length, OFFSET_CURRENT );
  283.             break;
  284.         }
  285.     }
  286.     while( !found_body && total_read < form_length );
  287.  
  288. return TRUE;
  289.  
  290. end:
  291. if ( iffhandle )
  292.     {
  293.     Close( iffhandle );
  294.     iffhandle = NULL;
  295.     }
  296. DisposeIFF();
  297. return FALSE;
  298. }
  299.  
  300.  
  301.  
  302. void AnyIFF( void )
  303. {
  304. BOOL success = FALSE;
  305. UWORD scaleN, scaleD, swidth, sheight, n, scaledw, scaledh;
  306. struct Rectangle *rect;
  307. UBYTE *address;
  308.  
  309. if ( DecodeILBM( filename ) )
  310.     {
  311.     rect = GETTXTOSCANRECT(dinfo);
  312.  
  313.     swidth = RECTANGLEWIDTH(rect);
  314.     if ( swidth <= bmhd.bmh_Width )
  315.         swidth = (iffbmap->BytesPerRow<<3) + 1;
  316.     sheight = RECTANGLEHEIGHT(rect);
  317.     if ( sheight <= bmhd.bmh_Height )
  318.         sheight = iffbmap->Rows + 1;
  319.  
  320.     if ( bmap = CreateBitMap( bmhd.bmh_Width, bmhd.bmh_Height, bmhd.bmh_Depth + (domask?1:0) ) )
  321.         {
  322.         if ( scalebmap = CreateBitMap( bmhd.bmh_Width, bmhd.bmh_Height, bmhd.bmh_Depth + (domask?1:0) ) )
  323.             {
  324.             myscaleinfo.bsi_SrcBitMap = iffbmap;
  325.             myscaleinfo.bsi_TempBitMap = scalebmap;
  326.             myscaleinfo.bsi_DestBitMap = bmap;
  327.  
  328.             if ( scr = OpenScreenTags( NULL,
  329.                 SA_DisplayID, DISPLAYID(dinfo) | ( displayID & EXTRA_HALFBRITE ),
  330.                 SA_Width, swidth,
  331.                 SA_Height, sheight,
  332.                 SA_Depth, bmhd.bmh_Depth,
  333.                 SA_Overscan, OSCAN_TEXT,
  334.                 SA_Type, CUSTOMSCREEN,
  335.                 SA_Quiet, TRUE,
  336.                 TAG_END ) )
  337.                 {
  338.                 register struct ViewPort *vp = &(scr->ViewPort);
  339.  
  340.                 success = TRUE;
  341.  
  342.                 SpritesOff();
  343.  
  344.                 address = iffctable;
  345.                 for ( n = 0; n < color_count; n++ )
  346.                     SetRGB4( vp, n, *address++>>4, *address++>>4, *address++>>4 );
  347.  
  348.                 frameptr = 0;
  349.  
  350.                 while( STILL_BLANKING )
  351.                     {
  352.                     do {
  353.                         scaleN = RangeRand( 32 ) + 1;
  354.                         scaleD = RangeRand( 32 ) + 1;
  355.                         } while ( scaleN >= scaleD );
  356.  
  357.                     myscaleinfo.bsi_HorNum = scaleN;
  358.                     myscaleinfo.bsi_VertNum = scaleN;
  359.                     myscaleinfo.bsi_HorDen = scaleD;
  360.                     myscaleinfo.bsi_VertDen = scaleD;
  361.  
  362.                     scaledw = scaleN * bmhd.bmh_Width / scaleD;
  363.                     scaledh = scaleN * bmhd.bmh_Height / scaleD;
  364.  
  365.                     left = RangeRand( swidth - scaledw - 1);
  366.                     top = RangeRand( sheight - scaledh - 1);
  367.  
  368.                     myscaleinfo.bsi_Flags = NULL;
  369.                     if ( RangeRand( 2 ) == 0 )
  370.                         myscaleinfo.bsi_Flags |= BSIF_INVERTHOR;
  371.                     ScaleBitMap ( &myscaleinfo );
  372.  
  373.                     if ( domask )
  374.                         BltMaskBitMapRastPort( bmap, 0, 0,
  375.                             &scr->RastPort, left, top,
  376.                             scaledw, scaledh, (ABC|ABNC|ANBC), (PLANEPTR)bmap->Planes[bmhd.bmh_Depth] );
  377.                     else
  378.                         BltBitMapRastPort( bmap, 0, 0,
  379.                             &scr->RastPort, left, top,
  380.                             scaledw, scaledh, 0xC0 );
  381.                     }
  382.                 SpritesOn();
  383.                 CloseScreen( scr );
  384.                 }
  385.             DisposeBitMap( scalebmap );
  386.             }
  387.         DisposeBitMap( bmap );
  388.         }
  389.     DisposeIFF();
  390.     }
  391.  
  392. if ( !success )
  393.     SendClientMsg( ACTION_FAILED );
  394. }
  395.  
  396.  
  397. /*void __main( char *line )*/
  398. void main( int argc, char **argv )
  399. {
  400. if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 36L ) )
  401.     {
  402.     if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library",0L ) )
  403.         {
  404.         if ( BitMapBase = OpenLibrary( "bitmap.library", 0L ) )
  405.             {
  406.             if ( GetPicName() )
  407.                 {
  408.                 if ( dinfo = OpenCommunication() )
  409.                     {
  410.                     RangeSeed = time( NULL );
  411.  
  412.                     AnyIFF();
  413.                     CloseCommunication( dinfo );
  414.                     }
  415.                 }
  416.             CloseLibrary( BitMapBase );
  417.             }
  418.         CloseLibrary( (struct Library *)GfxBase );
  419.         }
  420.     CloseLibrary( (struct Library *)IntuitionBase );
  421.     }
  422. }
  423.